// pages/detail/detail.js

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    src: "",
    information: "",
    height: "450rpx",
    icon: ""
  },
  collect: function(){
    var user = wx.getStorageSync('token')
    if(this.data.icon == "/images/collect.png"){
      this.setData({
        icon: "/images/collected.png"
      })
      wx.request({
        url: `${app.globalData.url}/collection`,
        method: "POST",
        header: {"content-type":"application/x-www-form-urlencoded"},
        data:{
          pId: this.data.pid,
          uId: user
        },
        success: res=>{
          console.log(res.data)
          console.log(this.data.pid)
        }
      })
    }else{
      this.setData({
        icon: "/images/collect.png"
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.pId;
    var user = wx.getStorageSync('token')
    console.log(user);
    this.setData({
      pid: id
    })
    wx.request({
      url: `${app.globalData.url}/pName/?pId=${id}`,
      success: res=>{
        console.log(res.data.data[0].pName)
        this.setData({
          name: res.data.data[0].pName,
          src: `${app.globalData.url}/pContent/?pId=${id}`
        })
      }
    })
    wx.request({
      url: `${app.globalData.url}/pInformation/?pId=${id}`,
      success: res=>{
        console.log(res.data.data[0].pInformation)
        this.setData({
          information: res.data.data[0].pInformation
        })
      }
    })
    wx.request({
      url: `${app.globalData.url}/collection/?uId=${user}`,
      success: res=>{
        var arr = res.data.data;
        console.log(res.data)
        for(var i=0; i<arr.length+1; i++){
          if(i==arr.length){
            this.setData({
              icon: "/images/collect.png"
            });
            break;
          }
          if(arr[i].pId == id){
            console.log(arr[i].pId)
            this.setData({
              icon: "/images/collected.png"
            });
            break;
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})